<?php /* SVN: $Id: admin_index.ctp 2754 2010-08-16 06:18:32Z boopathi_026ac09 $ */ ?>
<div class="transactions index js-response js-responses">
<ul class="filter-list-block clearfix ">
     		<li><?php echo $this->Html->link('<span>' .__l('All'). '</span>', array('controller'=>'transactions','action'=>'index','filter' => 'all'), array('escape' => false));?></span> </li>
     		<li><?php echo $this->Html->link('<span>' .__l('Admin'). '</span>', array('controller'=>'transactions','action'=>'index','filter' => 'admin'), array('escape' => false));?></span> </li>
</ul>
<div class="page-count-block clearfix">
<div class="grid_left">
	<?php echo $this->element('paging_counter');?>
</div>
<div class="grid_left">
		<?php echo $this->Form->create('Transaction' , array('action' => 'admin_index', 'type' => 'get', 'class' => 'normal search-form clearfix '));
        ?>
		<div class="grid_left">
   <?php
			$username = '';
			if(!empty($this->request->named['username'])) {
				$username = $this->request->named['username'];
			}
			?>
			<div class="mapblock-info">
			<?php echo $this->Form->autocomplete('Transaction.username', array('label' => __l('User'), 'acFieldKey' => 'Transaction.user_id', 'acFields' => array('User.username'), 'acSearchFieldNames' => array('User.username'), 'maxlength' => '255')); ?>
			     <div class="autocompleteblock">
			     </div>
	    	</div>
		</div>
		<div class="grid_left">
            <?php
			$project_id = '';
			if(!empty($this->request->named['project_id'])) {
				$project_id = $this->request->named['project_id'];
			}
			?>
            <div class="mapblock-info">
			    <?php echo $this->Form->autocomplete('Project.name', array('label' => __l('Project'), 'acFieldKey' => 'Transaction.project_id', 'acFields' => array('Project.name'), 'acSearchFieldNames' => array('Project.name'), 'maxlength' => '255')); ?>
			      <div class="autocompleteblock">
			      </div>
		    </div>
		</div>
		<?php
		if(!empty($this->request->named['user_id'])) {
			echo $this->Form->input('User.user_id',array('type' => 'hidden', 'value' => $this->request->named['user_id']));
		}
		echo $this->Form->submit(__l('Filter'));
		echo $this->Form->end();
		?>
	</div>
	</div>
	<table class="list">
		<tr>
			<th><div class="js-pagination"><?php echo $this->Paginator->sort(__l('Created'),'created');?></div> </th>
			<th class="dl"><div class="js-pagination"><?php echo $this->Paginator->sort(__l('User'),'user_id');?></div></th>
			<th class="dl"><div class="js-pagination"><?php echo __l('Message'); ?></div></th>
			<th class="dr"><div class="js-pagination credit round-3"><?php echo $this->Paginator->sort(__l('Credit'), 'amount').' ('.Configure::read('site.currency').')';?></div></th>
			<th class="dr"><div class="js-pagination debit round-3"><?php echo $this->Paginator->sort(__l('Debit'), 'amount').' ('.Configure::read('site.currency').')';?></div></th>
			</tr>
	<?php
	if (!empty($transactions)):

	$i = 0;
	$total_credit=0;
	$total_debit=0;
	foreach ($transactions as $transaction):
		$class = null;
		if ($i++ % 2 == 0) {
			$class = ' class="altrow"';
		}
	?>
		<tr<?php echo $class;?>>
			<td><?php echo $this->Html->cDateTimeHighlight($transaction['Transaction']['created']); ?></td>
			<?php $transaction['User']['UserAvatar'] = !empty($transaction['User']['UserAvatar']) ? $transaction['User']['UserAvatar'] : ''; ?>
			<td class="dl"><?php echo $this->Html->link($this->Html->showImage('UserAvatar', $transaction['User']['UserAvatar'], array('dimension' => 'micro_thumb', 'alt' => sprintf(__l('[Image: %s]'), $this->Html->cText($transaction['User']['username'], false)), 'title' => $this->Html->cText($transaction['User']['username'], false))), array('controller' => 'users', 'action' => 'view',  $transaction['User']['username'], 'admin' => false), array('escape' => false)); ?><?php echo $this->Html->link($this->Html->cText($transaction['User']['username'], false), array('controller' => 'users', 'action' => 'view', $transaction['User']['username'], 'admin' => false), array('escape' => false,'title'=>$this->Html->cText($transaction['User']['username'],false)));?></td>

			<td class="dl">
              <?php
                echo "<pre>";
                //print_r($transaction);
                echo "</pre>";
                echo $this->Html->transactionDescription($transaction);
                $reverse_statuses = array(
                                     ConstTransactionTypes::User,
                                     ConstTransactionTypes::Project,
                                     ConstTransactionTypes::UserCashWithdrawalAmount,
                                     ConstTransactionTypes::AcceptCashWithdrawRequest,
                                     ConstTransactionTypes::UserWithdrawalRequest,
                                     ConstTransactionTypes::AdminApprovedWithdrawalRequest,
                                     ConstTransactionTypes::AdminRejecetedWithdrawalRequest,
                                     ConstTransactionTypes::FailedWithdrawalRequest,
                                     ConstTransactionTypes::AmountRefundedForRejectedWithdrawalRequest,
                                     ConstTransactionTypes::AmountApprovedForUserCashWithdrawalRequest,
                                     ConstTransactionTypes::FailedWithdrawalRequestRefundToUser,
                                   );
               if($transaction['Transaction']['user_id'] == ConstUserIds::Admin)
               {
                  if(in_array($transaction['TransactionType']['id'],$reverse_statuses))
                  {
                    if($transaction['TransactionType']['is_credit'])
                    {
                     $transaction['TransactionType']['is_credit'] = 0;
                    }
                    else
                    {
                      $transaction['TransactionType']['is_credit'] = 1;
                    }
                  }
               }
             ?>
			</td>
			<td class="dr">
											<?php
                                                if($transaction['TransactionType']['is_credit'])
												{
												$total_credit=$total_credit+$transaction['Transaction']['amount'];
													echo $this->Html->cCurrency($transaction['Transaction']['amount']);
                                                  if(($transaction['Transaction']['payment_gateway_id'] == ConstPaymentGateways::PayPal) && ($transaction['TransactionType']['id'] == ConstTransactionTypes::AmountFundedToOwner))
													{
                                                      if($transaction['ProjectFund']['Project']['project_status_id'] == ConstProjectStatus::OpenForFunding)
                                                      {
                                                         echo __l(' (Authorized)');
                                                      }
                                                      else
                                                      {
                                                         echo __l(' (Captured)');
                                                      }
                                                    }
                                                    if(($transaction['Transaction']['payment_gateway_id'] == ConstPaymentGateways::Wallet) && ($transaction['TransactionType']['id'] == ConstTransactionTypes::AmountFundedToOwner))
													{
                                                      if($transaction['ProjectFund']['Project']['project_status_id'] == ConstProjectStatus::OpenForFunding)
                                                      {
                                                         echo __l(' (Pledged)');
                                                      }
                                                      else
                                                      {
                                                         echo __l(' (Funded)');
                                                      }
                                                    }
                                                    if(($transaction['Transaction']['payment_gateway_id'] == ConstPaymentGateways::PayPal) && ($transaction['TransactionType']['id'] == ConstTransactionTypes::Refunding || $transaction['TransactionType']['id'] == ConstTransactionTypes::Refunded))
													{
                                                      echo __l(' (Voided)');
                                                    }
                                                    else if(($transaction['Transaction']['payment_gateway_id'] == ConstPaymentGateways::Wallet) && ($transaction['TransactionType']['id'] == ConstTransactionTypes::Refunding || $transaction['TransactionType']['id'] == ConstTransactionTypes::Refunded))
                                                    {
                                                      echo __l(' (Refunded)');
                                                    }
                                                }
												else{
                                                     echo '--';
												}
											 ?>
										</td>
										<td class="dr">
											<?php
												if($transaction['TransactionType']['is_credit']):
													echo '--';
												else:
													$total_debit=$total_debit+$transaction['Transaction']['amount'];
													echo $this->Html->cCurrency($transaction['Transaction']['amount']);
													if(($transaction['Transaction']['payment_gateway_id'] == ConstPaymentGateways::PayPal) && ($transaction['TransactionType']['id'] == ConstTransactionTypes::Refunded || $transaction['TransactionType']['id'] == ConstTransactionTypes::Refunding))
													{
                                                      echo __l(' (Voided)');
                                                    }
                                                    else if(($transaction['Transaction']['payment_gateway_id'] == ConstPaymentGateways::Wallet) && ($transaction['TransactionType']['id'] == ConstTransactionTypes::Refunding || $transaction['TransactionType']['id'] == ConstTransactionTypes::Refunded))
                                                    {
                                                      echo __l(' (Refunded)');
                                                    }
                                                    if(($transaction['Transaction']['payment_gateway_id'] == ConstPaymentGateways::PayPal) && ($transaction['TransactionType']['id'] == ConstTransactionTypes::ProjectFunding || $transaction['TransactionType']['id'] == ConstTransactionTypes::Refunding))
													{
                                                      if($transaction['ProjectFund']['Project']['project_status_id'] == ConstProjectStatus::OpenForFunding)
                                                      {
                                                         echo __l(' (Authorized)');
                                                      }
                                                      else
                                                      {
                                                         echo __l(' (Captured)');
                                                      }
                                                    }
                                                    if(($transaction['Transaction']['payment_gateway_id'] == ConstPaymentGateways::Wallet) && ($transaction['TransactionType']['id'] == ConstTransactionTypes::AmountFundedToOwner))
													{
                                                      if($transaction['ProjectFund']['Project']['project_status_id'] == ConstProjectStatus::OpenForFunding)
                                                      {
                                                         echo __l(' (Pledged)');
                                                      }
                                                      else
                                                      {
                                                         echo __l(' (Funded)');
                                                      }
                                                    }
												endif;
											 ?>
										</td>
			</tr>

	<?php
		endforeach; ?>
		<tr class="total-block">
				<td class="dr" colspan="3"><span><?php echo __l('Total ');?></span></td>
				<td class="dr credit-total"><?php echo $this->Html->cCurrency($total_credit);?></td>
				<td class="dr debit-total"><?php echo $this->Html->cCurrency($total_debit);?></td>
			</tr>
	<?php else:
	?>
		<tr>
			<td colspan="11" class="notice"><?php echo __l('No Transactions available');?></td>
		</tr>
	<?php
	endif;
	?>
	</table>
	<div class="clearfix">
	<?php if (!empty($transactions)) : ?>
		<div class="js-pagination grid_right">
			<?php echo $this->element('paging_links'); ?>
		</div>
	<?php endif; ?>
</div>
</div>